<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline label-width="90px">
                <el-form-item label="状态" prop="status">
                    <el-select class="w-[280px]" style="width: 280px" v-model="queryParams.status" clearable placeholder="请选择部门状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.show_status" :key="index" :label="item.name" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="卡号" prop="account">
                    <el-input class="w-[280px]" v-model="queryParams.account" clearable placeholder="请输入卡号" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['card/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                导入
            </el-button>

            <el-button type="primary" @click="handleFaCard">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                分配
            </el-button>

            <el-button v-perms="['card/delete']" :disabled="!selectData.length" @click="handleDelete(selectData)"> 删除 </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="卡号" prop="account" />
                    <el-table-column label="分类" prop="card_type">
                        <template #default="{ row }">
                            <el-tag v-if="row.card_type == 2" type="success">虚拟卡</el-tag>
                            <el-tag v-if="row.card_type == 1" type="info">实体卡</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status">
                        <template #default="{ row }">
                            <el-tag v-if="row.status == 1" type="success">已激活</el-tag>

                            <el-tag v-if="row.status == 0" type="info">未激活</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="激活用户" prop="user.name">
                        <template #default="{ row }">
                            <div v-if="row.user">
                                <div class="flex pt-0">
                                    <el-image :src="row.user.avatar" fit="fill" :lazy="true" style="width: 50px"></el-image>
                                    <p>{{ row.user.nickname }} {{ row.user.mobile }}</p>
                                </div>
                                <div>
                                    <p>激活时间 : {{ row.update_time }}</p>
                                </div>
                            </div>
                            <div v-if="row.status == 0">未激活</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="归属商户" prop="merchant.name" />

                    <!-- <el-table-column label="价格" prop="price" /> -->
                    <el-table-column label="产品" prop="product_list" />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['card/delete']" type="danger" link @click="handleDelete(row.id)"> 删除 </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
        <import-popup ref="importRef" @success="getLists" />
    </div>
</template>

<script lang="ts" setup name="cardLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiCardLists, apiCardDelete } from '@/api/card'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

//导入
import ImportPopup from './import.vue'
const importRef = shallowRef<InstanceType<typeof EditPopup>>()

// 查询条件
const queryParams = reactive({
    status: '',
    account: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('show_status')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiCardLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    await nextTick()
    importRef.value?.open('add')
}

const handleFaCard = async () => {
    await nextTick()

    editRef.value?.open('add')
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiCardDelete({ id })
    getLists()
}

getLists()
</script>
